import java.util.*;
/**
 *  This class is the main class of the "World of Zuul" application. 
 *  "World of Zuul" is a very simple, text based adventure game.  Users 
 *  can walk around some scenery. That's all. It should really be extended 
 *  to make it more interesting!
 * 
 *  To play this game, create an instance of this class and call the "play"
 *  method.
 * 
 *  This main class creates and initialises all the others: it creates all
 *  rooms, creates the parser and starts the game.  It also evaluates and
 *  executes the commands that the parser returns.
 * 
 * @author  Michael Kölling and David J. Barnes
 * @version 2016.02.29
 */

public class Game 
{
    private Parser parser;
    private Room currentRoom;
    private Food bag = null;
    int energy = 150;
        
    /**
     * Create the game and initialise its internal map.
     */
    public Game() 
    {
        createRooms();
        parser = new Parser();
    }

    /**
     * Create all the rooms and link their exits together.
     */
    private void createRooms()
    {
        Room caihongdao, maoxiandaoshijie, linzhongzhicheng, jinyindao, shuixiashijie, bingfengxueyu, shijiantingzhizhihu, aideersitan, boss, xiaoheiwu,chuko;
      
        // create the rooms
       
        maoxiandaoshijie = new Room("你现在在冒险岛世界",new Food("单手剑",20));
        caihongdao = new Room("现在到了彩虹岛",new Food("大剑",-100));
        jinyindao = new Room("你到了金银岛",new Food("辅助武器",-10));
        linzhongzhicheng = new Room("你到了林中之城", new Food("套服", 20));
        bingfengxueyu = new Room("你现在到了冰峰雪域",null);
        shijiantingzhizhihu = new Room("你现在走到了时间停止之湖", new Food("HP恢复药水", 20));
        shuixiashijie = new Room("你到了水下世界",new Food("机甲手枪",-30));
        aideersitan = new Room("你走到了埃德尔斯坦", null);
        xiaoheiwu = new Room("恭喜你找到了小黑屋，得到了钥匙",new Food("矿石",20));
        chuko = new Room("你到到达了出口,恭喜你通关成功",null);
        boss = new Room("你现在在最后一关，打boss，你胜利了。快捡起掉落的物品",new Food("强化药水", 100));
        
        
        // initialise room exits
        maoxiandaoshijie.setExit("east", caihongdao);
        maoxiandaoshijie.setExit("west", linzhongzhicheng);
        maoxiandaoshijie.setExit("north", bingfengxueyu);
        caihongdao.setExit("east", jinyindao);
        caihongdao.setExit("west", maoxiandaoshijie);
        caihongdao.setExit("north", shuixiashijie);
        jinyindao.setExit("west", caihongdao);
        shuixiashijie.setExit("south", aideersitan);
        linzhongzhicheng.setExit("east", maoxiandaoshijie);
        linzhongzhicheng.setExit("north", shijiantingzhizhihu);
        shijiantingzhizhihu.setExit("south", linzhongzhicheng);
        shijiantingzhizhihu.setExit("east", bingfengxueyu);
        bingfengxueyu.setExit("west", shijiantingzhizhihu);
        bingfengxueyu.setExit("south", caihongdao);
        bingfengxueyu.setExit("north", aideersitan);
        aideersitan.setExit("south", bingfengxueyu);
        aideersitan.setExit("west", xiaoheiwu);
        aideersitan.setExit("east", chuko);
        aideersitan.setExit("north", boss);
        xiaoheiwu.setExit("east", aideersitan);
        boss.setExit("south", aideersitan);

        currentRoom = maoxiandaoshijie;  // start game outside
    }

    /**
     *  Main play routine.  Loops until end of play.
     */
    public void play() 
    {            
        printWelcome();

        // Enter the main command loop.  Here we repeatedly read commands and
        // execute them until the game is over.
                
        boolean finished = false;
        while (! finished) {
            Command command = parser.getCommand();
            finished = processCommand(command);
        }
            
        System.out.println("你的蓝量没有了，无法使用技能");
        
    }

    /**
     * Print out the opening message for the player.
     */
    private void printWelcome()
    {
        System.out.println();
        System.out.println("冒险岛需要你的探险");
        System.out.println("每一次探险都会扣除10点能量，包括（look，pick等）");
        System.out.println("背包容量不大，请合理使用");
        System.out.println("不是所有的东西都可以用的，会扣能量值的哦");
        System.out.println("需要提示请输入“help”");
        System.out.println();
        System.out.println(currentRoom.getDescription());
        currentRoom.printExits();
    }

    /**
     * Given a command, process (that is: execute) the command.
     * @param command The command to be processed.
     * @return true If the command ends the game, false otherwise.
     */
    private boolean processCommand(Command command) 
    {
        boolean wantToQuit = false;
        
        if(command.isUnknown()) {
            System.out.println("不好意思，真真小宝贝不明白噢");
            return false;
        }
        energy = energy - 10;
        Word commandWord = command.getCommandWord();
        switch(commandWord){
            case HELP:
                printHelp();
                break;
            case GO:              
                goRoom(command);
                break;
            case QUIT:
                wantToQuit = quit(command);
                break;
            case LOOK:
                Food food = currentRoom.getFood();
                if(food != null){
                    System.out.println("房间里有" + food.getName());
                }
                else{
                    System.out.println("这个房间里没有可以利用的东西噢");
                }
                System.out.println("当前能量值" + energy);
                break;
            case PICK:
                Food food1 = currentRoom.getFood();
                if(bag != null){
                    System.out.println("你的背包容量不够");
                }
                else{
                    System.out.println("你拾起了"+ food1.getName());
                    bag = food1;
                }
                System.out.println("当前能量值" + energy);
                break;
            case EAT:
                if(bag != null){
                    System.out.println("使用" + bag.getName());
                    Food food2 = currentRoom.getFood();
                    energy += food2.getEnergy(); 
                    System.out.println("当前能量值" + energy);
                    bag=null;
                }
                else{
                    System.out.println("没有可使用的东西");
                }
                break;
            case CHECK:
                if(bag != null){
                    System.out.println("背包有" + bag.getName());
                }
                else{
                    System.out.println("背包里什么都没有");
                }
                break;
        }
        
        if((energy < 0)||(energy == 0)) {
            wantToQuit = true;
        }

        return wantToQuit;
    }

    // implementations of user commands:

    /**
     * Print out some help information.
     * Here we print some stupid, cryptic message and a list of the 
     * command words.
     */
    private void printHelp() 
    {
        System.out.println("你现在有很多技能");
        System.out.println();
        Word[] words = Word.values();
        for(int i=0;i<words.length;i++){
            System.out.print(words[i].getCommandWord()+" ");            
        }
        System.out.println("请你选择你需要用的技能");
    }

    /** 
     * Try to go in one direction. If there is an exit, enter
     * the new room, otherwise print an error message.
     */
    private void goRoom(Command command) 
    {
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            //System.out.println("Go where?");
            System.out.println("到哪里去呢");
            return;
        }

        String direction = command.getSecondWord();

        // Try to leave current room.
        Room nextRoom = currentRoom.goNext(direction);
        if(nextRoom == null){
            //System.out.print("There is no door!");
            System.out.println("好像这条路不通噢宝贝");            
        }
        else {
            currentRoom = nextRoom;
            System.out.println(currentRoom.getDescription());
            currentRoom.printExits();
        }
       
       
    }
   
    
    /** 
     * "Quit" was entered. Check the rest of the command to see
     * whether we really quit the game.
     * @return true, if this command quits the game, false otherwise.
     */
    private boolean quit(Command command) 
    {
        if(command.hasSecondWord()) {
            System.out.println("Quit what?");
            return false;
        }
        else {
            return true;  // signal that we want to quit
        }
    }
}